/*
 * @lc app=leetcode.cn id=947 lang=javascript
 *
 * [947] 移除最多的同行或同列石头
 */

// @lc code=start
/**
 * @param {number[][]} stones
 * @return {number}
 */
class unionSet {
  constructor(n) {
    this.father = new Array(n).fill(0).map((val, i) => i);
    this.count = n;
  }
  find(x) {
    return this.father[x] === x ? x : this.find(this.father[x]);
  }
  merge(a, b) {
    const fa = this.find(a);
    const fb = this.find(b);
    if(fa === fb) return;
    this.father[fa] = fb;
    this.count--;
  }
}
var removeStones = function(stones) {
  const u = new unionSet(stones.length);
  for (let i = 0; i < stones.length; i++) {
    for (let j = i + 1; j < stones.length; j++) {
      let [x1, y1] = stones[i];
      let [x2, y2] = stones[j];
      if(x1 === x2 || y1 === y2) {
        u.merge(i, j);
      }
    }
  }
  return stones.length - u.count;
};
// @lc code=end

